home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Misc / Crossword / Source / PlainWord.m < prev    next >
Text File  |  1992-10-11  |  3KB  |  141 lines

  1. /*
  2.  
  3. File PlainWord.m
  4.  
  5. Letters and words lie at the heart of the crossword puzzle search.  The search currently proceeds one letter at a time; looking one word at a time can be construed as a special case.  Words have only one function: to keep track of the possibilities that remain.
  6.  
  7. */
  8.  
  9. #import <appkit/appkit.h>
  10. #import <stdlib.h>
  11.  
  12. #import "Plain.h"
  13. #import "Puzzle.h"
  14. #import "FunctionCache.h"
  15.  
  16.  
  17. /* ————————————————————————————————————————————————————————————————————————————  */
  18.  
  19.  
  20. @implementation PlainWord
  21.  
  22. - (countTable) getCount        {    return count;    }
  23.  
  24.  
  25. /* ————————————————————————————————————————————————————————————————————————————  */
  26.  
  27.  
  28. - initPuzzle: (id) thePuzzle
  29. {
  30.     [super  init];
  31.     puzzle = thePuzzle;
  32.     
  33.     return self;
  34. }
  35.  
  36.  
  37. - setSquares: (id) theSquares
  38. {
  39.     int        i;
  40.     
  41.     i = n = [squares = theSquares  count];
  42.     last = 0;
  43.     pattern[n] = '\0';
  44.     while (i--) pattern[i] = WILDCARD;
  45.     [self  update];
  46.     
  47.     return self;
  48. }
  49.  
  50.  
  51. - free
  52. {
  53.     [squares  free];
  54.     [super  free];
  55.     
  56.     return self;
  57. }
  58.  
  59.  
  60. /* ————————————————————————————————————————————————————————————————————————————  */
  61.  
  62.  
  63. - changeLetter: (int) i  to: (char) c
  64. {
  65.     pattern[last = i] = c;
  66.     [self  update];
  67.     
  68.     return self;
  69. }
  70.  
  71.  
  72. /* ————————————————————————————————————————————————————————————————————————————  */
  73.  
  74.  
  75. - update;
  76. {
  77.     id                state, dictionary;
  78.     id                match;
  79.     wordIndex        * index;
  80.     char            * key;
  81.     char            string [MAXLETTERS];
  82.     countTable        numbers;
  83.     int                i, j;
  84.     
  85.     state = [puzzle  getState];
  86.     dictionary = [puzzle  getDictionary];
  87.     
  88.     if (numbers = (countTable) [[state  getCountCache]  find: pattern])
  89.     {
  90.         for (i = 0; i < MAXLETTERS; i++)
  91.         for (j = 0; j < LETTERS; j++) count[i][j] = numbers[i][j];
  92.     }
  93.     
  94.     else if ((match = [dictionary  find: pattern  changeAt: last]))
  95.     {
  96.         for (i = 0; i < MAXLETTERS; i++)
  97.         for (j = 0; j < LETTERS; j++) count[i][j] = 0;
  98.     
  99.         i = [match  count];
  100.         while (i--)
  101.         {
  102.             [dictionary  read: string  word: WORD(match, i)  forLength: n];
  103.             [self  processWord: string];
  104.         }
  105.         
  106.         numbers = (countTable) malloc(sizeof(int) * LETTERS * MAXLETTERS);
  107.         key = (char *) malloc(n + 1);
  108.         strcpy(key, pattern);
  109.         
  110.         for (i = 0; i < MAXLETTERS; i++)
  111.         for (j = 0; j < LETTERS; j++) numbers[i][j] = count[i][j];
  112.         
  113.         [[state  getCountCache]  add: key  value: numbers];
  114.     }
  115.     
  116.     else
  117.     {
  118.         index = [dictionary  getIndex];
  119.         
  120.         for (i = 0; i < n; i++)
  121.         for (j = 0; j < LETTERS; j++)
  122.                     count[i][j] = (* index)[n].linkTable[j][i].n;
  123.     }
  124.     
  125.     [squares  makeObjectsPerform: @selector(update)];
  126.     return self;
  127. }
  128.  
  129.  
  130. - processWord: (char *) string
  131. {
  132.     int        j;
  133.     
  134.     for (j = 0; j < n; j++)
  135.                 count[j][string[j] - 'a']++;
  136.     
  137.     return self;
  138. }
  139.  
  140.  
  141. @end